<?php

include_once LIBRARIES_TOOLS_XTEMPLATE;
include_once LIBRARIES_ENTITIES_DATABASE;
include_once LIBRARIES_ENTITIES_ARTICLES;
include_once LIBRARIES_TOOLS_UTILITY;

class BlockNewArticleController {

    public function invoke() {

        $dir  = dirname(__FILE__);
        
        // get article list
        $rows = $this->getNewArticleList();

        // amount of article
        $amountOfArticle = count($rows);


        if (count($rows) > 0) {
            
            $xtemplate = new XTemplate($dir.'/views/new_article.html');

            $amountToView = 6;

            if ($amountOfArticle < $amountToView) {
                $amountToView = $amountOfArticle;
            }

            for ($i = 0; $i < $amountToView; $i++) {

                if ($rows[$i]->getStatus() == 1) {

                    // get days between createdDate and current date
                    $daysBetweenDate = Utility::daysBetweenDate($rows[$i]->getCreatedDate(), Utility::getCurrentDate());

                    // if days = 0 and days <= 3 then show new icon
                    if ($daysBetweenDate <= 3) {
                        $xtemplate->assign("TITLE_ARTICLE", Utility::subString($rows[$i]->getTitle(), 70, true));
                        $xtemplate->assign("DATE_ARTICLE", $rows[$i]->getCreatedDate());
                        $xtemplate->assign("ALIAS_ARTICLE", $rows[$i]->getAlias());
                        $xtemplate->assign("ID_ARTICLE", $rows[$i]->getID());
                        $xtemplate->parse("main.new_news_list");
                    }
                }
            }

            $xtemplate->parse("main");
            $xtemplate->out("main");
        }
    }

    
    // get article list
    private function getNewArticleList() {
        $db = new Database();
        try {
            $article = new Articles();
            $rows = $article->getNewList($db);
            return $rows;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

}

?>
